﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using IMCOM.Imagiality.Core;
using IMCOM.Imagiality.ImageProcessing.Canvas2D;

namespace IMCOM.Imagiality.MainGUI
{
    public class ScanImagesGUI : Panel
    {
        private CanvasImageWrapper canvasImageWrapper;
        private Panel panelScanLevel;
        private Label labelScanLevel;
        private NumericUpDown numericUpDownScanLevel;
        private Button buttonTakeScanImages;
        private ListBox listBoxScanImages;
        private Button buttonCleanList;
        private IList<ScanImagesGUIListener> listeners;

        public CanvasImageWrapper CanvasImageWrapper
        {
            get { return canvasImageWrapper; }
            set { SetCanvasImageWrapper(value); }
        }

        public int ScanLevel
        {
            get { return (int)numericUpDownScanLevel.Value; }
        }

        public ScanImagesGUI()
        {
            listeners = new List<ScanImagesGUIListener>();

            Width = 150;
            Height = 200;

            panelScanLevel = new Panel();
            Controls.Add(panelScanLevel);

            labelScanLevel = new Label();
            panelScanLevel.Controls.Add(labelScanLevel);
            labelScanLevel.Text = "Scan Level";
            labelScanLevel.TextAlign = ContentAlignment.MiddleLeft;

            numericUpDownScanLevel = new NumericUpDown();
            panelScanLevel.Controls.Add(numericUpDownScanLevel);

            buttonTakeScanImages = new Button();
            Controls.Add(buttonTakeScanImages);
            buttonTakeScanImages.Text = "Take Scan Images";

            listBoxScanImages = new ListBox();
            Controls.Add(listBoxScanImages);

            buttonCleanList = new Button();
            Controls.Add(buttonCleanList);
            buttonCleanList.Text = "Clean List";

            Resize += new EventHandler(GUIResized);
            numericUpDownScanLevel.ValueChanged += new EventHandler(NumericUpDownScanLevelChanged);
            buttonTakeScanImages.Click += new EventHandler(ButtonTakeScanImagesClicked);
            listBoxScanImages.SelectedValueChanged += new EventHandler(listBoxScanImagesSelectedValueChanged);
            buttonCleanList.Click += new EventHandler(ButtonCleanListClicked);

            ArrangeLayout();
        }

        private void SetCanvasImageWrapper(CanvasImageWrapper canvasImageWrapper)
        {
            this.canvasImageWrapper = canvasImageWrapper;
            listBoxScanImages.Items.Clear();

            for (int i = 0; i < canvasImageWrapper.OriginalImages.Count; i++)
            {
                listBoxScanImages.Items.Add("Level");
            }

            listBoxScanImages.SelectedIndex = canvasImageWrapper.ActiveImageIndex;

            foreach (ScanImagesGUIListener listener in listeners)
            {
                listener.canvasImageWrapperChanged();
            }
        }

        public void AddListener(ScanImagesGUIListener listener)
        {
            listeners.Add(listener);
        }

        private void GUIResized(object sender, System.EventArgs e)
        {
            ArrangeLayout();
        }

        private void ArrangeLayout()
        {
            panelScanLevel.Location = new Point(5, 5);
            panelScanLevel.Width = Width - 10;
            panelScanLevel.Height = numericUpDownScanLevel.Height;

            labelScanLevel.Location = new Point(0, 0);
            labelScanLevel.Width = (panelScanLevel.Width / 2);

            numericUpDownScanLevel.Location = new Point(panelScanLevel.Width / 2, 0);
            numericUpDownScanLevel.Width = (panelScanLevel.Width / 2);

            buttonTakeScanImages.Location = new Point(5, panelScanLevel.Bottom + 5);
            buttonTakeScanImages.Width = Width - 10;

            listBoxScanImages.Location = new Point(5, buttonTakeScanImages.Bottom + 5);
            listBoxScanImages.Width = Width - 10;

            buttonCleanList.Location = new Point(5, listBoxScanImages.Bottom + 5);
            buttonCleanList.Width = Width - 10;
        }

        private void NumericUpDownScanLevelChanged(object sender, System.EventArgs e)
        {
            foreach (ScanImagesGUIListener listener in listeners)
            {
                listener.scanLevelNumericUpDownValueChanged(sender, e);
            }
        }

        private void ButtonTakeScanImagesClicked(object sender, System.EventArgs e)
        {
            foreach (ScanImagesGUIListener listener in listeners)
            {
                listener.takeScanImagesButtonClicked(sender, e);
            }
        }

        private void listBoxScanImagesSelectedValueChanged(object sender, System.EventArgs e)
        {
            canvasImageWrapper.ActiveImageIndex = listBoxScanImages.SelectedIndex;

            foreach (ScanImagesGUIListener listener in listeners)
            {
                listener.scanImagesListBoxSelectedValueChanged(sender, e);
                listener.canvasImageWrapperChanged();
            }
        }

        private void ButtonCleanListClicked(object sender, System.EventArgs e)
        {
            canvasImageWrapper = null;
            listBoxScanImages.Items.Clear();

            foreach (ScanImagesGUIListener listener in listeners)
            {
                listener.cleanListButtonClicked(sender, e);
            }
        }
    }
}
